<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

//class CompanyExport implements FromCollection , WithHeadings, WithMapping, ShouldAutoSize, WithDrawings, WithEvents, FromView
class CompanyExport implements FromView
{
    private $data;

    public function view(): View
    {
        foreach ($this->data as &$item) {
            $logo = "./upload/company_logo/". $item['logo'];
            if (!$item['logo'] || !file_exists(public_path($logo))) {
                $item['logo_url'] = './upload/default/default_company.png';
            } else {
                $item['logo_url'] = $logo;
            }
        }
        return view('company.index', [
            'invoices' => $this->data
        ]);
    }

    public function drawings()
    {
        $len = count($this->data);
        $arr_drawing = [];
        for($i=0; $i < $len; $i++) {
            if ($logo = $this->data[$i]['logo'])
                $logo =  public_path($logo);

            if (!file_exists($logo)) {
                $logo = public_path('/image/default_company.png');
            }

            $drawing = new Drawing();
            $drawing->setName('Logo');
            $drawing->setPath($logo);
            $drawing->setHeight(20);
            $drawing->setCoordinates('I' . ($i+2));
            array_push($arr_drawing, $drawing);
        }
        return $arr_drawing;
    }

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $len = count($this->data);
                for ($i = 0; $i < $len; $i++) {
                    // 将第一行行高设置为20
                    $event->sheet->getDelegate()->getRowDimension(($i+2))->setRowHeight(100  );
                    $event->sheet->getDelegate()->getRowDimension(($i+2));
                }
            },
        ];
    }




    public function __construct($data)
    {
        $this->data = $data;
    }

    public function headings() : array
    {
        return [
            '公司logo',
            '公司名称',
            '公司类型 ',
            '公司规模',
            '公司地址',
            '公司介绍 ',
            '岗位数量',
            '排序编号',
        ];
    }

    public function map($invoice): array
    {
        $aa =  [
            '',
            $invoice['title'],
            $invoice['type']['value'],
            $invoice['num'] ?: '0',
            $invoice['address'],
            $invoice['desc'],
            $invoice['position_count'] ?: '0',
            $invoice['sort'] ?: '0'
        ];
        return $aa;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return new Collection($this->data);
    }
}
